package com.yzt.zhuju.dialog.gift;

import android.content.DialogInterface;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.dokimet.basedialog.CenterDialog;
import com.yzt.zhuju.R;
import com.yzt.zhuju.widget.widget.ImageLoadManger;
import com.yzt.zhuju.widget.widget.SVGALoadPlayer;
import com.yzt.p_base.utils.DisplayUtils;
import com.opensource.svgaplayer.SVGACallback;
import com.opensource.svgaplayer.SVGAImageView;

/**
 * User: fish
 * E-mail: 709913163@qq.com
 * Date: 2023/1/4
 * Time: 13:55
 * describe:礼物动画页 播放svga
 */
public class GiftAnimDialog extends CenterDialog {

    private FrameLayout layout_anim;

    private String path;

    private int pathType; //1-svgaPath 2-gifPath 3-webpPath

    @Override
    public int getLayoutId() {
        return R.layout.dialog_gift_anim;
    }

    @Override
    public void initView(View v) {
        if (getArguments() == null) {
            return;
        }
        path = getArguments().getString("url");
        pathType = getArguments().getInt("pathType");
        layout_anim = findViewById(R.id.layout_anim);
        if (pathType == 1) {
            playSVGA();
        } else {
            playGif();
        }
    }

    private void playSVGA() {
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(DisplayUtils.getDisplayPxWidth(), DisplayUtils.getDisplayPxHeight());
        layoutParams.gravity = Gravity.CENTER;
        SVGAImageView svgaImageView = new SVGAImageView(getContext());
        svgaImageView.setLoops(1);
        svgaImageView.setClearsAfterStop(true);
        layout_anim.addView(svgaImageView, layoutParams);
        svgaImageView.setCallback(new SVGACallback() {
            @Override
            public void onStep(int i, double v) {
            }

            @Override
            public void onRepeat() {
            }

            @Override
            public void onFinished() {
                dismiss();
            }

            @Override
            public void onPause() {
            }
        });
        SVGALoadPlayer.loadSvga(svgaImageView, path, new SVGALoadPlayer.OnLoadSvgaListener() {
            @Override
            public void onLoadSuccess() {

            }

            @Override
            public void onLoadEnd() {
                dismiss();
            }
        });
    }

    private void playGif() {
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(DisplayUtils.getDisplayPxWidth(), DisplayUtils.getDisplayPxHeight());
        layoutParams.gravity = Gravity.CENTER;
        ImageView imageView = new ImageView(getContext());
        layout_anim.addView(imageView, layoutParams);
        ImageLoadManger.loadGifIvOnce(mActivity, path, imageView, new ImageLoadManger.OnPlayGifListener() {
            @Override
            public void onPlayStart() {

            }

            @Override
            public void onPlayEnd() {
                dismiss();
            }
        });
    }

    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        super.onDismiss(dialog);
        if (layout_anim != null) {
            layout_anim.removeAllViews();
        }
    }

    @Override
    public int getHeight() {
        return WindowManager.LayoutParams.MATCH_PARENT;
    }
}
